﻿using System;
using System.Collections.Generic;
using SpCop.Core;
using SpCop.Core.Model;
using SpCop.Model;

namespace SpCop.General.Receivers
{
    public class FeatureReceiversShouldNotUseElevations : RuleBase
    {
        

        private IList<Problem> _problems;
        private Package _package;

        public override IList<Problem> Check(Package package)
        {

            _package = package;
            _problems = base.Check(package);

            VisitCollection(package.Features,Visit);

           
            return _problems;
        }


        public override void Visit(Feature item)
        {
            base.Visit(item);
        }

        //public override void Visit(Feature item)
        //{
        //    if(!string.IsNullOrEmpty(item.ReceiverTypeName))
        //    {
        //        // _problems.Add(new Problem(GetResolution("ReceiverNotFound",item.ReceiverTypeName, item.Title)));

               
        //        try
        //        {
        //             TypeNode typeNode = GetReceiverTypeNode(item);

        //             //CheckFeatureActivatedOnType(item, typeNode, "FeatureActivated");
        //             //CheckFeatureActivatedOnType(item, typeNode, "FeatureDeactivating");
        //             //CheckFeatureActivatedOnType(item, typeNode, "FeatureInstalled");
        //             //CheckFeatureActivatedOnType(item, typeNode, "FeatureUninstalling");
        //        }
        //        catch(Exception exception)
        //        {
        //            Console.WriteLine("Failed to analyse method body ! {0}",exception.Message);
        //            AddProblem("UnableToResolve",exception.Message);

        //        }
        //    }


        //    base.Visit(item);
        //}

       

        //private void CheckFeatureActivatedOnType(Feature item, TypeNode typeNode, string methodName)
        //{
        //    var featureActivatedMethods = typeNode.GetMembersNamed(Identifier.For(methodName));

        //    if (featureActivatedMethods.Count == 0)
        //    {
        //        // TODO Analyse Base type feature
        //        if (typeNode.BaseType.Name.Name != "SPFeatureReceiver")
        //        {
        //           CheckFeatureActivatedOnType(item, typeNode.BaseType, methodName);
        //        }
        //        else
        //        {
                    
        //        }
        //}
        //    else
        //    {
        //        var activationMethod = featureActivatedMethods[0] as Method;

        //        var visitor = new ElevationVisitor();
        //        visitor.Visit(activationMethod);

        //        if (visitor.UsesRunWithElevatedPrivileges)
        //        {
        //           AddProblem("UsingElevation", item.Title, methodName);

        //        }
        //    }
        }
    
}
